import { Link } from 'react-router-dom';
import { Button, Space } from 'antd';
import {
  CheckCircleOutlined,
  ClockCircleOutlined,
  ExclamationCircleOutlined,
  PlayCircleOutlined
} from '@ant-design/icons';
import type { SelectProps } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { getOptionLabel } from '../../data/statusData';

const columns = (booleanOptions: SelectProps['options'], appTypes: SelectProps['options'],
  taskFrequencies: SelectProps['options'], executeStatuses: SelectProps['options']) => {
  const columns: ColumnsType<any> = [
    {
      title: '#',
      key: '__row_number',
      align: 'right',
      width: 48,
      render: (text, record, index) => {
        return (
          <Link to={`/task_list/task_detail/${record.task.id}`}>
            <Button
              type="link"
              style={{ padding: 0 }}
            >
              <em>{index + 1}</em>
            </Button>
          </Link>
        );
      }
    },
    {
      title: '标题',
      key: 'title',
      render: (text, record, index) => record.task.title
    },
    {
      title: '用户',
      key: 'userId',
      render: (text, record, index) => record.user.username
    },
    {
      title: '可用',
      key: 'active',
      align: 'center',
      render: (text, record, index) => getOptionLabel(booleanOptions, record.task.active.toString())
    },
    {
      title: '来源公司',
      key: 'sourceCompanyId',
      render: (text, record, index) => record.sourceCompany?.name || ''
    },
    {
      title: '来源应用类型',
      key: 'sourceAppType',
      render: (text, record, index) => getOptionLabel(appTypes, record.task.sourceAppType)
    },
    {
      title: '来源部门',
      key: 'sourceDepartmentName',
      render: (text, record, index) => record.task.sourceDepartmentName || '全公司'
    },
    {
      title: '目标公司',
      key: 'targetCompanyId',
      render: (text, record, index) => record.targetCompany?.name || ''
    },
    {
      title: '目标应用类型',
      key: 'targetAppType',
      render: (text, record, index) => getOptionLabel(appTypes, record.task.targetAppType)
    },
    {
      title: '目标部门',
      key: 'targetDepartmentName',
      render: (text, record, index) => record.task.targetDepartmentName || '全公司'
    },
    {
      title: '包含子部门',
      key: 'includingChildren',
      align: 'center',
      render: (text, record, index) => getOptionLabel(booleanOptions, record.task.includingChildren.toString())
    },
    {
      title: '包含雇员',
      key: 'includingEmployees',
      align: 'center',
      render: (text, record, index) => getOptionLabel(booleanOptions, record.task.includingEmployees.toString())
    },
    {
      title: '删除不存在的子部门',
      key: 'deleteNonExistentChildren',
      align: 'center',
      render: (text, record, index) => getOptionLabel(booleanOptions, record.task.deleteNonExistentChildren.toString())
    },
    {
      title: '同步频率',
      key: 'frequency',
      align: 'center',
      render: (text, record, index) => getOptionLabel(taskFrequencies, record.task.frequency)
    },
    {
      title: '执行天',
      key: 'day',
      render: (text, record, index) => {
        if (record.task.frequency === 'Monthly') {
          text = record.task.dayOfMonth;
          return text > 0 ? ('每月第' + text + '天') : ('每月倒数第' + Math.abs(text) + '天');
        } else if (record.task.frequency === 'Weekly') {
          text = record.task.dayOfWeek;
          return '每周' + '日一二三四五六'.charAt(text);
        } else {
          return '-';
        }
      }
    },
    {
      title: '执行时间',
      key: 'hour_minute',
      align: 'center',
      render: (text, record, index) => {
        const hour = (record.task.hour >= 10 ? '' : '0') + record.task.hour;
        const minute = (record.task.minute >= 10 ? '' : '0') + record.task.minute;
        return hour + ':' + minute;
      }
    },
    {
      title: '最大重试次数',
      key: 'maxRetryCount',
      align: 'right',
      render: (text, record, index) => record.task.maxRetryCount || '-'
    },
    {
      title: '重试间隔时间',
      key: 'retryInterval',
      align: 'right',
      render: (text, record, index) => record.task.retryInterval ? (record.task.retryInterval + '分钟') : '-'
    },
    {
      title: '任务创建时间',
      key: 'createdAt',
      render: (text, record, index) => record.task.createdAt
    },
    {
      title: '执行批次数',
      key: 'batchCount',
      align: 'right',
      render: (text, record, index) => record.task.batchCount
    },
    {
      title: '成功执行批次数',
      key: 'successBatchCount',
      align: 'right',
      render: (text, record, index) => record.task.successBatchCount
    },
    {
      title: '最近任务执行状态',
      key: 'lastExecuteStatus',
      render: (text, record, index) => (
        <Space size={5}>
          {
            record.task.lastExecuteStatus === 'Created' ? (
              <ClockCircleOutlined style={{ color: 'rgba(0,0,0,0.45)' }} />
            ) : (record.task.lastExecuteStatus === 'Started' ? (
              <PlayCircleOutlined style={{ color: '#faad14' }} />
            ) : (record.task.lastExecuteStatus === 'Ended' ? (
              <CheckCircleOutlined style={{ color: '#52c41a' }} />
            ) : (record.task.lastExecuteStatus === 'Error' ? (
              <ExclamationCircleOutlined style={{ color: '#ff4d4f' }} />
            ) : null)))
          }
          {getOptionLabel(executeStatuses, record.task.lastExecuteStatus)}
        </Space>
      )
    },
    {
      title: '最近任务重试次数',
      key: 'lastRetryCount',
      align: 'right',
      render: (text, record, index) => record.task.lastRetryCount
    },
    {
      title: '最近任务计划时间',
      key: 'lastStartTime',
      render: (text, record, index) => record.task.lastPlanTime ? record.task.lastPlanTime.substr(0, 10) : ''
    },
    {
      title: '最近任务开始时间',
      key: 'lastStartTime',
      render: (text, record, index) => record.task.lastStartTime
    },
    {
      title: '最近任务结束时间',
      key: 'lastEndTime',
      render: (text, record, index) => record.task.lastEndTime
    }
  ];
  return columns;
};

export default columns;
